/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cdn.service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dotnet.common.utils.DateUtils;
import com.shishuo.cdn.dao.UserDao;
import com.shishuo.cdn.entity.LoginLog;
import com.shishuo.cdn.entity.User;
import com.shishuo.cms.entity.Admin;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.exception.AuthException;
import com.shishuo.cms.util.AuthUtils;
import com.shishuo.cms.util.PropertyUtils;

/**
 * 管理员
 * 
 * @author Administrator
 * 
 */
@Service
public class UserService {

	@Autowired
	private UserDao userDao;

	// ///////////////////////////////
	// ///// 增加 ////////
	// ///////////////////////////////

	/**
	 * 添加管理员
	 * 
	 * @param email
	 * @param name
	 * @param password
	 * @return Admin
	 */
	public Admin addAdmin(String name, String password)
			throws AuthException {
		Date now = new Date();
		Admin admin = new Admin();
		admin.setName(name);
		admin.setPassword(AuthUtils.getPassword(password));
		admin.setCreateTime(now);
		//adminDao.addAdmin(admin);
		return admin;
	}

	// ///////////////////////////////
	// ///// 刪除 ////////
	// ///////////////////////////////

	/**
	 * 删除管理员
	 * 
	 * @param adminId
	 * @return Integer
	 */
	public int deleteAdmin(long adminId) {
		//return adminDao.deleteAdmin(adminId);
		return 1;
	}

	// ///////////////////////////////
	// ///// 修改 ////////
	// ///////////////////////////////

	/**
	 * 修改管理员资料
	 * 
	 * @param adminId
	 * @param name
	 * @param password
	 * @param status
	 * @return Admin
	 * @throws AuthException
	 */

	public void updateAdminByAmdinId(long adminId, String password)
			throws AuthException {
		String pwd = AuthUtils.getPassword(password);
		//adminDao.updateAdminByadminId(adminId, pwd);
	}

	// ///////////////////////////////
	// ///// 查詢 ////////
	// ///////////////////////////////

	/**
	 * 管理员登陆
	 * 
	 * @param email
	 * @param password
	 * @param request
	 * @throws IOException
	 */
	public void adminLogin(String name, String password,
			HttpServletRequest request) throws AuthException,
			IOException {
		/*AdminVo admin = adminDao.getAdminByName(name);
		if (admin == null) {
			throw new AuthException("邮箱或密码错误");
		}
		String loginPassword = AuthUtils.getPassword(password);
		if (loginPassword.equals(admin.getPassword())) {
			HttpSession session = request.getSession();
			admin.setPassword("");
			if (name.equals(PropertyUtils
					.getValue("shishuocms.admin"))) {
				admin.setAdmin(true);
			} else {
				admin.setAdmin(false);
			}
			session.setAttribute(SystemConstant.SESSION_ADMIN,
					admin);
		} else {
			throw new AuthException("邮箱或密码错误");
		}*/
	}

	/**
	 * 通过Id获得指定管理员资料
	 */
	public User getUserById(String userId) {
		return userDao.getUserById(userId);
	}
	
	/**
	 * 记录用户登录日志信息
	 * @param loginLog
	 * @return
	 */
	public boolean addLoginLog(LoginLog loginLog){
		return userDao.addLoginLog(loginLog);
	}

	/**
	 * 获得所有管理员的分页数据
	 * 
	 * @param offset
	 * @param rows
	 * @return List<Admin>
	 */
	public List<Admin> getAllList(long offset, long rows) {
		//return adminDao.getAllList(offset, rows);
		return null;
	}

	/**
	 * 获得所有管理员的数量
	 * 
	 * @return Integer
	 */
	public int getAllListCount() {
		//return adminDao.getAllListCount();
		return 1;
	}

	/**
	 * 获得所有管理员的分页
	 * 
	 * @param Integer
	 * @return PageVo<Admin>
	 */
	public PageVo<Admin> getAllListPage(int pageNum) {
		PageVo<Admin> pageVo = new PageVo<Admin>(pageNum);
		pageVo.setRows(20);
		List<Admin> list = this.getAllList(pageVo.getOffset(),
				pageVo.getRows());
		pageVo.setList(list);
		pageVo.setCount(this.getAllListCount());
		return pageVo;
	}

	/**
	 * 通过email获得管理员资料
	 * 
	 * @param email
	 * @return Admin
	 */
	public Admin getAdminByName(String name) {
		//return adminDao.getAdminByName(name);
		return null;
	}

	public long getSuperAdminId() {
		Admin admin = getAdminByName(PropertyUtils
				.getValue("shishuocms.admin"));
		return admin.getAdminId();
	}
	
	public boolean addUser(User user){
		int i = userDao.addUser(user);
		if(i>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 修改密码
	 * @param password
	 * @return
	 */
	public boolean updateUserById(String password,String userId){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("password", password);
		map.put("userId", userId);
		int i = userDao.updateUserById(map);
		if(i>0){
			return true;
		}
		return false;
	}
	
	/**
	 * 存储验证码 by liaoliyun
	 * @param yanzhengma
	 * @return
	 */
	public boolean updateYzmById(String yanzhengma,String userId){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("yanzhengma", yanzhengma);
		map.put("userId", userId);
		int i = userDao.updateYzmById(map);
		if(i>0){
			return true;
		}
		return false;
	}
	/**
	 * 存储邮件验证码 by liaoliyun
	 * @param mailyzm
	 * @return
	 */
	public boolean updateMailYzmById(String mailyzm,String userId){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("mailyanzhengma", mailyzm);
		map.put("userId", userId);
		int i = userDao.updateMailYzmById(map);
		if(i>0){
			return true;
		}
		return false;
	}	
	/**
	 * 查询关联用户的银行卡信息
	 * @param userId
	 * @return
	 */
	public User getUserBankById(String userId){
		
		return userDao.getUserBankById(userId);
	}
	
	/**
	 * 更新用户资料信息
	 * @param user
	 * @return
	 */
	public boolean updateUserInfo(User user){
		List<Map<String,Object>> list = userDao.getBankById(user.getUser_id().toString());
		if(null==list||list.size()==0){
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("user_id", user.getUser_id());
			map.put("addtime", new Date());
			map.put("bank_type",1);
			userDao.addUserBank(map);
		}
		int i = userDao.updateUserInfo(user);
		if(i>0){
			return true;
		}
		return false;
	}
	
	public static void main(String[] args) {
		cf(1);
	}

	private static void cf(int j) {
		if(j>9)return ;
		for(int i=1;i<=j;i++)
			System.out.print(i==j?" "+i +"*"+  j +" = "+(i*j)+"\n":" "+i +"*"+  j +" = "+(i*j)+"\t");
//		for(int k=1;k<=9-j;k++)
//			{System.out.println("      ");
//			if(k==(9-j))System.out.println("|\n");
//			}
		cf(j+1);
	}
	
	/** 个人中心--设置支付密码 **/
	public boolean setPayPwd(String payPwd,String userId){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("zhifumima", payPwd);
		map.put("userId", userId);
		int i = userDao.setPayPwd(map);
		if(i>0){
			return true;
		}
		return false;
	}
	
	/** 个人中心--设置支付密码 **/
	public boolean setPhoneYzmCount(String userId,String type){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("type", type);
		int i = userDao.setPhoneYzmCount(map);
		if(i>0){
			return true;
		}
		return false;
	}
	
	/** 个人中心--设置支付密码 **/
	public boolean setPhoneYzmDate(String userId,Date date){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("date", date==null?null:DateUtils.getCurrentDateStrByFormat("yyyy-MM-dd"));
		map.put("userId", userId);
		int i = userDao.setPhoneYzmDate(map);
		if(i>0){
			return true;
		}
		return false;
	}
}
